package work.timejay.user.service.users.impl;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import work.timejay.common.SuperServiceImpl;
import work.timejay.user.entity.mqs.MqLog;
import work.timejay.user.entity.users.UserAmount;
import work.timejay.user.mapper.users.UserAmountMapper;
import work.timejay.user.service.mqs.MqLogService;
import work.timejay.user.service.users.UserAmountService;
import work.timejay.user.vo.UserSend;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author wwj
 * @since 2022-01-03
 */
@Service
public class UserAmountServiceImpl extends SuperServiceImpl<UserAmountMapper, UserAmount> implements UserAmountService {
    @Autowired
    private MqLogService mqLogService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAmount(UserSend userSend) {
        //幂等性
        MqLog one = mqLogService.lambdaQuery().eq(MqLog::getTxNo, userSend.getTxNo()).one();
        if(null != one){
            return;
        }
        UserAmount userAmount = lambdaQuery().eq(UserAmount::getId, userSend.getId()).one();
        userAmount.setAmount(userAmount.getAmount()-userSend.getAmount());
        updateById(userAmount);
        //添加事务记录
        mqLogService.addRecord(userSend);
//        if (true) {
//            throw new RuntimeException("人为异常");
//        }
    }
}
